package ca.paulshin.yunatube.main;

import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import ca.paulshin.yunatube.BaseSubMenuFragment;
import ca.paulshin.yunatube.R;
import ca.paulshin.yunatube.YunaTubeApplication;
import ca.paulshin.yunatube.aboutyuna.SubMenuAboutYunaFragment;
import ca.paulshin.yunatube.aboutyuna.WebViewFragment;
import ca.paulshin.yunatube.aboutyunatube.AboutYunaTubeFragment;
import ca.paulshin.yunatube.common.Constants;
import ca.paulshin.yunatube.common.Preference;
import ca.paulshin.yunatube.common.Utils;
import ca.paulshin.yunatube.database.YuNaTubeSqliteDatabase;
import ca.paulshin.yunatube.http.FlickrDataLoader;
import ca.paulshin.yunatube.image.album.SubMenuAlbumFragment;
import ca.paulshin.yunatube.image.gifs.SubMenuGifs;
import ca.paulshin.yunatube.links.SubMenuLinksFragment;
import ca.paulshin.yunatube.news.News;
import ca.paulshin.yunatube.quiz.QuizFragment;
import ca.paulshin.yunatube.search.Search;
import ca.paulshin.yunatube.toyuna.ToYunaFragment;
import ca.paulshin.yunatube.youtube.SubMenuYoutubeFragment;

public class MainMenuFragment extends BaseSubMenuFragment implements OnClickListener {

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_menu_fragment, null);

		RelativeLayout home = (RelativeLayout) view.findViewById(R.id.supermenu_home);
		home.setOnClickListener(this);

		RelativeLayout aboutYuNa = (RelativeLayout) view.findViewById(R.id.supermenu_about_yuna);
		aboutYuNa.setOnClickListener(this);

		RelativeLayout news = (RelativeLayout) view.findViewById(R.id.supermenu_yuna_news);
		news.setOnClickListener(this);

		RelativeLayout youtube = (RelativeLayout) view.findViewById(R.id.supermenu_youtube);
		youtube.setOnClickListener(this);

		RelativeLayout album = (RelativeLayout) view.findViewById(R.id.supermenu_album);
		album.setOnClickListener(this);
		
		RelativeLayout gifs = (RelativeLayout) view.findViewById(R.id.supermenu_gifs);
		gifs.setOnClickListener(this);

		RelativeLayout search = (RelativeLayout) view.findViewById(R.id.supermenu_search);
		search.setOnClickListener(this);

		RelativeLayout links = (RelativeLayout) view.findViewById(R.id.supermenu_links);
		links.setOnClickListener(this);
		
		RelativeLayout toYuna = (RelativeLayout) view.findViewById(R.id.supermenu_to_yuna);
		toYuna.setOnClickListener(this);

		RelativeLayout survey = (RelativeLayout) view.findViewById(R.id.supermenu_survey);
		survey.setOnClickListener(this);
		
		RelativeLayout quiz = (RelativeLayout) view.findViewById(R.id.supermenu_quiz);
		quiz.setOnClickListener(this);

		RelativeLayout aboutYuNaTube = (RelativeLayout) view.findViewById(R.id.supermenu_about_yunatube);
		aboutYuNaTube.setOnClickListener(this);

		RelativeLayout share = (RelativeLayout) view.findViewById(R.id.supermenu_share);
		share.setOnClickListener(this);

		return view;
	}
	
	private void switchMenuFragment(Fragment newFragment) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter, R.anim.fragment_slide_right_exit);
		ft.replace(R.id.menu_frame, newFragment, "detailFragment");
		 ft.addToBackStack(null);
		ft.commit();
	}

	@Override
	public void onClick(View v) {
		MainActivity activity = (MainActivity)getActivity();
		
		switch (v.getId()) {
		case R.id.supermenu_home:
			activity.setFragment(new MainContentFragment(), false);
			break;
			
		case R.id.supermenu_about_yuna:			
			switchMenuFragment(new SubMenuAboutYunaFragment());
			break;
			
		case R.id.supermenu_to_yuna:
			activity.setFragment(new ToYunaFragment(), false);
			break;
			
		case R.id.supermenu_yuna_news:
			Dialog news = new News(getActivity());
			news.show();
			break;
			
		case R.id.supermenu_youtube:
//			if (Preference.getBoolean(YuNaTubeSqliteDatabase.IS_DB_AVAILABLE))
				switchMenuFragment(new SubMenuYoutubeFragment());
			break;
			
		case R.id.supermenu_album:
			if (!Utils.isNetworkAvailable())
				Utils.showToast(getActivity(), R.string.message_network_unavailable);
			else {
				YunaTubeApplication application = (YunaTubeApplication) Utils.ctx;
				if (application.collectionList == null) {
					if (Utils.isNetworkAvailable()) {
						new AsyncTask<String, Void, Integer>() {							
							@Override
							protected Integer doInBackground(String... params) {
								// Load Flickr
								YunaTubeApplication application = (YunaTubeApplication) Utils.ctx;
								application.collectionList = FlickrDataLoader.getInstance().getCollectionList();
								return application.collectionList.size();
							}
							
							@Override
							protected void onPostExecute(Integer result) {
								if (result > 0)
									switchMenuFragment(new SubMenuAlbumFragment());
							};
						}.execute();
					}
					else
						Utils.showToast(getActivity(), R.string.message_network_unavailable);
				}
				else
					switchMenuFragment(new SubMenuAlbumFragment());
			}
			break;
			
		case R.id.supermenu_gifs:
			switchMenuFragment(new SubMenuGifs());
			break;
			
		case R.id.supermenu_search:
			Dialog searchDialog = new Search(MainMenuFragment.this.getActivity());
			searchDialog.show();
			break;
			
		case R.id.supermenu_links:
			switchMenuFragment(new SubMenuLinksFragment());
			break;
			
		case R.id.supermenu_survey:
			String deviceId = Secure.getString(getActivity().getContentResolver(), Secure.ANDROID_ID);
			activity.setFragment(WebViewFragment.getInstance(R.string.supermenu_survey, Constants.SURVEY_URL + deviceId), false);
			break;
			
		case R.id.supermenu_quiz:
			activity.setFragment(new QuizFragment(), false);
			break;
			
		case R.id.supermenu_about_yunatube:
			activity.setFragment(new AboutYunaTubeFragment(), false);
			break;
			
		case R.id.supermenu_share:
			Utils.share(getActivity(), getString(R.string.supermenu_share), getString(R.string.supermenu_share_subject), getString(R.string.supermenu_share_text));
			break;
		}
	}
}
